<?php 

class NickServ extends AthemePHP {
	
	function NickServ($xmlURL)
	{
		$this->xmlURL = $xmlURL;
		$this->include = false;
	}
	
	/**
	 * @author rbonkestoter
	 * @email rick@globalirc.net
	 * 
	 * @param $username
	 * @param $password
	 * @param $emailaddress
	 * @return result (int)
	 * 
	 * @syntax $class->NickServ->Register('username', 'password', 'email');
	 */
	function Register($username, $password, $emailaddress)
	{
		// Lets create a param list!
		$this->addParam('NICKSERV');
		$this->addParam('REGISTER');
		$this->addParam($username);
		$this->addParam($password);
		$this->addParam($emailaddress);
		
		$result = $this->doCMD();
		
				
		if($result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'])
		{
			$retRes = $result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'];
		} else {
			$retRes = 90;
		}
		
		
		return $retRes;
	}
	
	/**
	 * @author rbonkestoter
	 * @email rick@globalirc.net
	 *  
	 * @param $nick
	 * @param $password
	 * @param $athemeid
	 * @return unknown_type
	 */
	function Drop($nick, $password, $athemeid)
	{
		$this->addParam('NICKSERV');
		$this->addParam('DROP');
		$this->addParam($nick);
		$this->addParam($password);
		
		$result = $this->doCMD($nick, $athemeid);
		
		if($result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'])
		{
			$retRes = $result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'];
		} else {
			$retRes = 90;
		}
		
		return $retRes;
	}
	
	/**
	 * 
	 * @param $nick
	 * @param $password
	 * @param $athemeid
	 * @return unknown_type
	 */
	function ChangePassword($nick, $password, $athemeid)
	{
		$this->addParam('NICKSERV');
		$this->addParam('SET');
		$this->addParam('PASSWORD');
		$this->addParam($password);
		
		$result = $this->doCMD($nick, $athemeid);
		
		if($result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'])
		{
			$retRes = $result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'];
		} else {
			$retRes = 90;
		}
		
		return $retRes;
	}
	
	/**
	 * 
	 * @param unknown_type $nick
	 * @param unknown_type $athemeid
	 * @param unknown_type $property
	 * @param unknown_type $value
	 * @return unknown_type
	 */
	function Property($nick, $athemeid, $property, $value)
	{
		$this->addParam('NICKSERV');
		$this->addParam('SET');
		$this->addParam('PROPERTY');
		$this->addParam($property);
		$this->addParam($value);
		
		$result = $this->DoCommand($nick, $athemeid, 'atheme.command');
		
		if($result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'])
		{
			$retRes = $result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'];
		} else {
			$retRes = 90;
		}
		
		return $retRes;
	}
	
	/**
	 * 
	 * @param $nick
	 * @param $command
	 * @param $paramarray
	 * @param $athemeid
	 * @return unknown_type
	 */
	function Set($nick, $command, $paramarray, $athemeid)
	{
		$this->addParam('NICKSERV');
		$this->addParam('SET');
		$this->addParam($command);
		for($i = 0; $i < count($paramarray); $i++)
		{
			$this->addParam($paramarray[$i]);
		}
		
		$result = $this->doCMD($nick, $athemeid);
	}
	
	/*
	 * Login and Logout routine!
	 */
	
	/**
	 * 
	 * @param $username
	 * @param $password
	 * @return Atheme Auth Cookie! (or error code!)
	 * 
	 * Error codes:
	 * 
	 *       fault 1 - insufficient parameters
	 *       fault 3 - account is not registered
	 *       fault 5 - invalid username and password
	 *       fault 6 - account is frozen
	 */
	function Login($username, $password)
	{
		$xmlCall = "<?xml version=\"1.0\"?>
<methodCall>
   <methodName>atheme.login</methodName>
   <params>
         <param>
         <value><string>".$username."</string></value>
         </param>
				 <param>
         <value><string>".$password."</string></value>
         </param>
         <param>
         <value><string>".$_SERVER['REMOTE_ADDR']."</string></value>
         </param>
      </params>
   </methodCall>";
		
		$response = $this->get_response($xmlCall);
		
		$json = XML_unserialize($response);
		//print_r($json);
		
		if($json['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'])
		{
			return $json['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'];
		}
		else
		{
			return $json['methodResponse']['params']['param']['value']['string'];
		}
	}
	
	/**
	 * 
	 * @param $username
	 * @param $athemeid
	 * @return unknown_type
	 * 
	 * Error codes
	 * 
	 * 		 fault 1 - insufficient parameters
 	 *       fault 3 - unknown user
 	 *       fault 5 - validation failed
 	 *       default - success message
	 */	
	function Logout($username, $athemeid)
	{
		$xmlCall = "<?xml version=\"1.0\"?>
<methodCall>
   <methodName>atheme.logout</methodName>
   <params>
         <param>
         <value><string>".$athemeid."</string></value>
         </param>
				 <param>
         <value><string>".$username."</string></value>
         </param>
      </params>
   </methodCall>";
		
		$response = $this->get_response($xmlCall);
		
		if($json['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'])
		{
			return $result['methodResponse']['fault']['value']['struct']['member'][0]['value']['int'];
		}
		else
		{
			return $json['methodResponse']['params']['param']['value']['string'];
		}
		
	}
}
?>